package com.b1502.store2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.b1502.store2.R;
import com.b1502.store2.model.bean.CategoryListShowBean;
import com.b1502.store2.util.ImageLoaderUtils;
import com.b1502.store2.util.UrlUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 作者：张宏达
 * 类的创建时间： 2017/2/28 15:56
 * 类的作用：分类列表展示页面做xrecyclerview的适配器类
 */

public class CategoryListShowAdapter extends RecyclerView.Adapter<CategoryListShowAdapter.CategoryListShowViewHolder> {

    //声明变量
    List<CategoryListShowBean.ProductsBean> productsBeen;
    com.nostra13.universalimageloader.core.DisplayImageOptions DisplayImageOptions;
    LayoutInflater inflater;
    Context context;

    public CategoryListShowAdapter(Context context) {
        this.context = context;
        DisplayImageOptions = ImageLoaderUtils.initOptions();
    }


    //添加数据的方法
    public void addData(List<CategoryListShowBean.ProductsBean> productsBeen) {
        this.productsBeen = productsBeen;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public CategoryListShowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_category_list, parent, false);
        CategoryListShowViewHolder holder = new CategoryListShowViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(CategoryListShowViewHolder holder, int position) {

        holder.tv_categorylistshow_item_shopname.setText(productsBeen.get(position).getName());
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.displayImage(UrlUtil.getImageUrl(productsBeen.get(position).getImageUrl()), holder.iv_categorylistshow_item, DisplayImageOptions);
        holder.tv_categorylistshow_item_price.setText("¥" + productsBeen.get(position).getMemberPrice());
    }

    @Override
    public int getItemCount() {
        return productsBeen.size();
    }

    public class CategoryListShowViewHolder extends RecyclerView.ViewHolder {

        //声明变量
        ImageView iv_categorylistshow_item;
        TextView tv_categorylistshow_item_price;
        TextView tv_categorylistshow_item_shopname;

        public CategoryListShowViewHolder(View itemView) {
            super(itemView);

            //找控件
            iv_categorylistshow_item = (ImageView) itemView.findViewById(R.id.iv_categorylistshow_item);
            tv_categorylistshow_item_shopname = (TextView) itemView.findViewById(R.id.tv_categorylistshow_item_shopname);
            tv_categorylistshow_item_price = (TextView) itemView.findViewById(R.id.tv_categorylistshow_item_price);
        }
    }
}
